import java.util.List;
import java.util.LongSummaryStatistics;

public class MySingleList implements ILinkedList{
    static class ListNode{
        public int val;
        public ListNode next;
        public ListNode(int val){
            this.val = val;
        }
    }
    public ListNode head;
    public void creatList(){
        ListNode listNode1 = new ListNode(12);
        ListNode listNode2 = new ListNode(23);
        ListNode listNode3 = new ListNode(34);
        ListNode listNode4 = new ListNode(45);
        ListNode listNode5 = new ListNode(56);
        listNode1.next = listNode2;
        listNode2.next = listNode3;
        listNode3.next = listNode4;
        listNode4.next = listNode5;
        head = listNode1;
    }
    @Override
    public void addFirst(int data) {
        ListNode node = new ListNode(data);
        node.next = head;
        head = node;
    }

    @Override
    public void addLast(int data) {
        ListNode node = new ListNode(data);
        if(head == null){
            head = node;
            return;
        }
        ListNode cur = head;
        while(cur.next != null){
            cur = cur.next;
        }
        cur.next = node;
    }

    @Override
    public void addIndex(int index, int data) {
        checkpos(index);
        if(index == 0){
            addFirst(data);
            return;
        }
        if(index == size()){
            addLast(data);
            return;
        }
        ListNode node = new ListNode(data);
        ListNode cur = findIndex(index);
        node.next = cur.next;
        cur.next = node;
    }
    private ListNode findIndex(int index){
        ListNode cur = head;
        int count = 0;
        while(count != index - 1){
            cur = cur.next;
            count++;
        }
        return cur;
    }
    private void checkpos(int index){
        if(index < 0 || index > size()){
            throw new CheckPosException("index的位置出现异常");
        }
    }

    @Override
    public boolean contains(int key) {
        ListNode cur = head;
        while(cur != null){
            if(cur.val == key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    @Override
    public void remove(int key) {
        if(head == null){
            System.out.println("链表为空");
        }
        if(head.val == key){
            head = head.next;
            return;
        }
        ListNode cur = search(key);
        if(cur == null){
            System.out.println("没有这个数据");
            return ;
        }
        ListNode del = cur.next;
        cur.next = del.next;
    }
    private ListNode search(int key){
        ListNode cur = head;
        while(cur.next != null){
            if(cur.next.val == key){
                return cur;
            }
            cur = cur.next;
        }
        cur = null;
        return null;
    }

    @Override
    public void removeAllKey(int key) {
        if(head == null){
            return;
        }
        ListNode pre = head;
        ListNode cur = head.next;
        while(cur != null){
            if(cur.val == key){
                pre.next = cur.next;
                cur = cur.next;
            }else{
                pre = cur;
                cur = cur.next;
            }
        }
        if(head.val == key){
            head = head.next;
        }
    }

    @Override
    public int size() {
        int count = 0;
        ListNode cur = head;
        while(cur != null){
            count++;
            cur = cur.next;
        }
        return count;
    }

    @Override
    public void clear() {
        ListNode cur = head;
        while(cur != null){
            ListNode curN = cur.next;
            cur.next = null;
            cur = curN;
        }
        head = null;
    }

    @Override
    public void display() {
        ListNode cur = head;
        while(cur != null){
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    public ListNode reverseList(){
        if(head == null){
            return null;
        }
        if(head.next == null){
            return head;
        }
        ListNode cur = head.next;
        head.next = null;
        while(cur != null){
            ListNode cuN = cur.next;
            cur.next = head;
            head = cur;
            cur = cuN;
        }
        return head;
    }
    public ListNode middleMode(){
        ListNode fast = head;
        ListNode slow = head;

        while(fast != null && fast.next != null){
            fast = fast.next.next;
            slow = slow.next;
        }
        return slow;
    }
    public int kthTolast(int key){
        if(key < 0 || key >size()){
            return -1;
        }
        int count = 0;
        ListNode fast = head;
        ListNode lower = head;
        while(count < key - 1){
            fast = fast.next;
            count++;
        }
        while(fast.next != null){
            fast = fast.next;
            lower = lower.next;
        }
        return lower.val;
    }
    public ListNode mergeTwoList(ListNode headA, ListNode headB){
        if(headA != null && headB == null){
            return headA;
        }
        if(headA == null && headB != null){
            return headB;
        }
        if(headA == null && headB == null){
            return null;
        }
        ListNode newH = new ListNode(-1);
        ListNode tmp = newH;
        while(headA != null && headB != null){
            if(headA.val )
        }
    }
}
